import {
	getScheduleInfo
} from '../../../../../api/weddingMan/index'
const NAVLIST = [{
	id: 0,
	name: '作品'
}]
Page({
	data: {
		scheduleList: [],
		scheduleTotal: 0,
		name: '',
		userId: ''
	},
	onLoad: function(options) {
		;
		this.setData({
			userId: options.userId,
			name: options.name
		})
		wx.setNavigationBarTitle({
			title: `${this.data.name}的档期`
		})
		getScheduleInfo(this.data.userId).then((res) => {
			;
			this.handleScheduleList(res.data.list);
		}).catch((err) => {
			;
		});
	},
	onShow: function() {},
	onUnload: function() {},
	handleScheduleList(pList) {
		this.data.scheduleList.splice(0);
		const tmpLen = pList.length;
		let tmpScheduleTotal = this.data.scheduleTotal;
		for (let i = 0; i < tmpLen; i += 1) {
			let tmpYear = pList[i].date.split('-')[0];
			let tmpMonth = pList[i].date.split('-')[1];
			tmpScheduleTotal += pList[i].size;
			if (tmpMonth.split('')[0] === '0') {
				// 去0
				tmpMonth = tmpMonth.split('')[1]
			}
			this.handleScheduleData(tmpYear, tmpMonth, pList[i].dataList);
		}
		this.setData({
			scheduleTotal: tmpScheduleTotal
		});
		;
		;
	},
	handleScheduleData(pYear, pMonth, pDayList) {
		// 判断scheduleList中是否包含year为tmpYear的项，没有就新增
		const tmpLen = this.data.scheduleList.length;
		const tmpScheduleList = this.data.scheduleList;
		let currentYearIndex = -1;
		let tmpIndex = -1;
		for (let i = 0; i < tmpLen; i += 1) {
			if (tmpScheduleList[i].year === pYear) {
				tmpIndex = i;
				currentYearIndex = i;
				break;
			}
		}
		if (tmpIndex === -1) {
			tmpScheduleList.push({
				year: pYear,
				dataList: []
			})
			currentYearIndex = tmpScheduleList.length - 1;
			// this.setData({
			// 	scheduleList: tmpScheduleList
			// })
		}
		// 判断tmpYear对应的dataList中是否包含month为tmpMonth的项，没有就新增
		const currentYearDataList = tmpScheduleList[currentYearIndex].dataList;
		const tmpLen2 = currentYearDataList.length;
		let tmpIndex2 = -1;
		let currentMonthIndex = -1;
		for (let i = 0; i < tmpLen2; i += 1) {
			if (currentYearDataList[i].month === pMonth) {
				tmpIndex2 = i;
				currentMonthIndex = i;
				break;
			}
		}
		if (tmpIndex2 === -1) {
			currentYearDataList.push({
				month: pMonth,
				dataList: []
			})
			currentMonthIndex = currentYearDataList.length - 1;
		}
		// dayList数据加入到当前月份
		const currentMonthDataList = currentYearDataList[currentMonthIndex].dataList;
		const tmpLen3 = pDayList.length;
		for (let i = 0; i < tmpLen3; i += 1) {
			let tmpDay = pDayList[i].date.split('-')[2];
			if (tmpDay.split('')[0] === '0') {
				tmpDay = tmpDay.split('')[1];
			}
			currentMonthDataList.push({
				day: tmpDay,
				...pDayList[i],
			});
		}
		this.setData({
			scheduleList: tmpScheduleList
		})
	}
});
